<?php

import("data.db.DbAbstractTable");
import("data.db.DbFactory");

class DbUnit extends DbAbstractTable {
	
	var $className = "data.logic.product.Unit";
	const CLASS_NAME =  "data.logic.product.Unit";
	
	//
	// SQL Table description section
	//
	const TB_TABLENAME	= "pd_unit";
	const TB_I			= 'unit';
	
	const TB_NAME		= 'unit_name';
	const TB_DESCRIPT	= 'unit_descript';
	const TB_CLASS_PROP	= 'unit_class';
	
	function _get_TABLENAME() {return self::TB_TABLENAME;}
	function _get_ID() {return self::TB_I."_id";}
	function _get_SUFFIX() {return self::TB_I;}
	//
	//
	
	function backwardForeignCollection() {
		return array("UnitProperty"=>"data.logic.property.UnitProperty");
	}
	
	// properties
	var $name;
	var $description;
	var $classProp;
	
	public function __construct($noinit=false) {
		parent::__construct($noinit);
	}
	
	function getName() {
		return $this->name;
	}
	function setName($name) {
		$this->name = $name;
	}
	
	function getDescription() {
		return $this->description;
	}
	function setDescription($desc) {
		$this->description = $desc;
	}
	
	function getClassProp() {
		return $this->classProp;
	}
	function setClassProp($class) {
		$this->classProp = $class;
	}
	
	function setUnitProperties(&$props) {
		$this->backwardForeigns['UnitProperty']->setList($props);
	}
	function getUnitProperties() {
		return $this->backwardForeigns['UnitProperty']->getList();
	}
	
	// restore
	function fillEntity($arrResult) {
		parent::fillEntity($arrResult);
		$this->setName($arrResult[self::TB_NAME]);
		$this->setDescription($arrResult[self::TB_DESCRIPT]);
		$this->setClassProp($arrResult[self::TB_CLASS_PROP]);
	}
	
	//store
	function fillFields() {
		parent::fillFields();
		$this->fildsForStore[self::TB_NAME] = "'".$this->getName()."'";
		$this->fildsForStore[self::TB_DESCRIPT] = "'".$this->getDescription()."'";
		$this->fildsForStore[self::TB_CLASS_PROP] = "'".$this->getClassProp()."'";
	}
	
	// static find
	function findAllByGroupId($group) {
		return parent::__select(
			"unitg_id_unit=unit_id and unitg_id_grpn='".$group."'",
			0, 0, "pd_unit_group",
			'data.logic.product.Unit');
	}
	
	// same for all classes
	public static function findAll() {return parent::findAll(self::CLASS_NAME);}
    public static function findById($id) {return parent::findById($id, self::CLASS_NAME);}
}
?>